package com.example.demo.cas;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLongFieldUpdater;

public class TestCount {

    public static void main(String[] args) throws InterruptedException {
        CountDownLatch cdl = new CountDownLatch(10);
        AtomicInteger ai = new AtomicInteger(0);
        for (int i =0;i<10;i++){
            new Thread(()->{
                for (int j = 0;j<1000;j++){
                    ai.incrementAndGet();
                }
                cdl.countDown();
            }).start();
        }
        cdl.await();
        System.out.println(ai.get());
    }

}


class TestCount2{

    public static void main(String[] args) throws InterruptedException {
       CountDownLatch cdl = new CountDownLatch(100);
       Count c = new Count();
       for (int i =0;i<100;i++){
           new Thread(()->{
               for (int j = 0;j<1000;j++){
                   c.add();
               }
               cdl.countDown();
           }).start();
       }
       cdl.await();
       System.out.println(c.count);
    }
}



class Count{
    public volatile long count;

     static final AtomicLongFieldUpdater<Count> updater = AtomicLongFieldUpdater.newUpdater(Count.class,"count");


    public void add(){
        updater.incrementAndGet(this);
    }

}